﻿using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Module.ParameterSettings
{
    public partial class Announcement : BaseUI.BasePage
    {
        private SYS_ANNOUNCEMENTBLL amentBLL = new SYS_ANNOUNCEMENTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "n_id";
                ViewState["OrderDire"] = "ASC";
                btnQuery_Click(sender, e);
            }
        }

        #region 增删改查

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string whereStr = "";

            //发布公告公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND T1.N_COMPANYID= " + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @"  and T1.N_COMPANYID in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            //状态
            if (Convert.ToDecimal(FORM_STATE.SelectedValue.ToString()) != -1)
            {
                whereStr += " AND T1.n_state = " + Convert.ToDecimal(FORM_STATE.SelectedValue.ToString());
            }
            //发布人
            if (txtName.Text.Trim().Trim() != "")
            {
                whereStr += " AND T1.c_name LIKE '%" + txtName.Text.Trim() + "%'";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var strId = DELETE_CHECK_IDS.Value;

            var sql = "DELETE FROM SYS_ANNOUNCEMENT WHERE N_ID IN(" + strId + ")";
            if (amentBLL.NoQuery(sql) > 0)
            {
                MessageBox.Show(this, "共" + strId.Split(',').Length + "条数据删除成功！");
            }
            else
            {
                MessageBox.Show(this, "删除失败！");
            }
            BindData(false);
        }

        #endregion 增删改查

        #region 代码块

        protected void BindData(bool iffirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = " AND T1.N_COMPANYID= " + FORM_COMPANY_ID.Value;
            }

            int rowcount = 0;
            if (iffirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = amentBLL.GetRecordCount(Where, lognUser.USER_NAME);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = amentBLL.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 标题列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            //查询
            btnQuery_Click(sender, e);
        }

        #endregion 代码块
    }
}